package com.up.blog.service;
import com.up.blog.domain.InsertArticlesInfo;
import com.up.blog.domain.UpdateArticlesInfo;
import com.up.blog.mapper.MessageMapper;
import com.up.blog.mapper.UpdateArticlesMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
@Service("updateArticlesService")
public class UpdateArticlesService {
//    InputStream conf = Resources.getResourceAsStream("SqlMapConfig.xml");
//    //创建会话工厂
//    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(conf);
//    //使用会话工厂创建会话对象
//    SqlSession sqlSession = sqlSessionFactory.openSession(true);
//    UpdateArticlesMapper mapper = sqlSession.getMapper(UpdateArticlesMapper.class);
@Autowired
private UpdateArticlesMapper mapper;
    public UpdateArticlesService() throws IOException {
    }

    public int update(String aname,String author,String content,Integer id) throws SQLException {
        UpdateArticlesInfo updateArticlesInfo=new UpdateArticlesInfo();
        updateArticlesInfo.setAname(aname);
        updateArticlesInfo.setAuthor(author);
        updateArticlesInfo.setContent(content);
        updateArticlesInfo.setId(id);
        return mapper.update(updateArticlesInfo);
    }
    public int delete(Integer id) throws SQLException {
        return mapper.delete(id);
    }
    public int publish(String title,String author,String time,String tag,String text) throws SQLException {
        InsertArticlesInfo insertArticlesInfo =new InsertArticlesInfo();
        insertArticlesInfo.setAuthor(author);
        insertArticlesInfo.setTag(tag);
        insertArticlesInfo.setText(text);
        insertArticlesInfo.setTitle(title);
        insertArticlesInfo.setTime(time);
        return mapper.publish(insertArticlesInfo);
    }
    public int draft(String title,String author,String time,String tag,String text) throws SQLException {
        InsertArticlesInfo insertArticlesInfo =new InsertArticlesInfo();
        insertArticlesInfo.setAuthor(author);
        insertArticlesInfo.setTag(tag);
        insertArticlesInfo.setText(text);
        insertArticlesInfo.setTitle(title);
        insertArticlesInfo.setTime(time);
        return mapper.draft(insertArticlesInfo);
    }
}
